package com.xiaode.controller.app.secretProtection;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.xiaode.interceptor.AppLoginInterceptor;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

/**
 * 密保服务
 */
@Path("/secretProtection")
@Api("密保相关服务")
@EnableCORS
public class SecretProtectionController extends Controller {
	 @Inject
	 private SecretProtectionService service;
	 
	 
	 /**
	  * 获取密保问题
	  */
	 @ApiOper("获取密保问题")
	 @Clear({AppLoginInterceptor.class})
	 public void getAnswer(@NotBlank @ApiPara(value = "account", notes = "账号")String account) throws Exception{
		 Ret ret = service.getAnswer(account);
		 renderJson(ret);
	 }
	 
	 /**
	  * 验证密保
	  */
	 @ApiOper("验证密保")
	 @Clear({AppLoginInterceptor.class})
	 public void getVerificationAnswer( 
			 @NotBlank @ApiPara(value = "account", notes = "学籍号")String account,
			 @NotBlank @ApiPara(value = "problem1", notes = "密保问题1") String problem1,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer1", notes = "密保答案1") String answer1,
			 @NotBlank @ApiPara(value = "problem2", notes = "密保问题2") String problem2,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer2", notes = "密保答案2") String answer2,
			 @NotBlank @ApiPara(value = "problem3", notes = "密保问题3") String problem3,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer3", notes = "密保答案3") String answer3 ) throws Exception{
		 Ret ret = service.getVerificationAnswer(account, answer1, answer2, answer3);
		 renderJson(ret);
	 }
	 
	 /**
	  * 通过密保修改密码
	  */
	 @ApiOper("通过密保修改密码")
	 @Clear({AppLoginInterceptor.class})
	 public void setPasswordByProtection(
			 @NotBlank @ApiPara(value = "account", notes = "学籍号")String account,
			 @Size(max = 40) @ApiPara(value = "password", notes = "新的密码") String password,
			 @NotEmpty @ApiPara(value = "answerCode", notes = "验证时返回的码") Long answerCode) throws Exception{
		 Ret ret = service.setPasswordByProtection(account, answerCode, password);
		 renderJson(ret);
	 }

	 /**
	  * 添加密保
	  */
	 @ApiOper("添加密保")
	 public void addSecretProtection(
			 @NotBlank @ApiPara(value = "problem1", notes = "密保问题1") String problem1,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer1", notes = "密保答案1") String answer1,
			 @NotBlank @ApiPara(value = "problem2", notes = "密保问题2") String problem2,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer2", notes = "密保答案2") String answer2,
			 @NotBlank @ApiPara(value = "problem3", notes = "密保问题3") String problem3,
			 @Size(min = 1,max = 30) @ApiPara(value = "answer3", notes = "密保答案3") String answer3 ) throws Exception{
		 Integer userType = getAttr("userType");
		 String account = getAttr("account");
		 Ret ret = service.addSecretProtection(account, userType, problem1, problem2, problem3, answer1, answer2, answer3);
		 renderJson(ret);
	 }
	 
	 /**
	  * 单纯的查看密保是否存在
	  */
	 @ApiOper("单纯的查看密保是否存在")
	 public void isExistence() throws Exception{
		 String account = getAttr("account");
		 Ret ret = service.isExistence(account);
		 renderJson(ret);
	 }
	 
	 /**
	  * 查看密保信息
	  */
	 @ApiOper("查看密保信息")
	 public void getSecretProtectionInfo() throws Exception{
		 String account = getAttr("account");
		 Ret ret = service.getSecretProtectionInfo(account);
		 renderJson(ret);
	 }
	
	 /**
	  * 获取密保选择内容
	  */
	 @ApiOper("获取密保选择内容")
	 public void getProblemInfo() throws Exception{
		 Ret ret = service.getProblemInfo();
		 renderJson(ret);
	 }
}
